if (FALSE) {
### Save code in a file "demo.r" and run with 2 processors by
### SHELL> mpiexec -np 2 Rscript demo.r
spmd.code <- "
### Initialize
suppressMessages(library(pbdMPI, quietly = TRUE))
### Check.
if(comm.size() != 2){
comm.stop(\"2 processors are requried.\")
}
### Manually distributed iris.
da <- iris[get.jid(nrow(iris)),]
### Dump data.
comm.write.table(da, file = \"iris.txt\", quote = FALSE, sep = \"\\t\",
row.names = FALSE)
### Read back in.
da.gbd <- comm.read.table(\"iris.txt\", header = TRUE, sep = \"\\t\",
quote = \"\")
comm.print(c(nrow(da), nrow(da.gbd)), all.rank = TRUE)
### Read in common.
da.common <- comm.read.table(\"iris.txt\", header = TRUE, sep = \"\\t\",
quote = \"\", read.method = \"common\")
comm.print(c(nrow(da.common), sum(da.common != iris)))
### Finish.
finalize()
"
# execmpi(spmd.code, nranks = 2L)
}
Run the code above in your browser using DataLab